package com.weipai.education.service.impl;

import com.weipai.education.dao.InventoryDao;
import com.weipai.education.dao.ProductCateDao;
import com.weipai.education.dao.ProductDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.enums.StatusUtils;
import com.weipai.education.model.ProductGoods;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.InventoryService;
import com.weipai.education.utils.GenerateUserAccountNumberUtils;
import com.weipai.education.utils.OneBarcodeUtil;
import com.weipai.education.utils.PropUtils;
import com.weipai.education.utils.SessionUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/13 15:07
 */
@Service
public class InventoryServiceImpl implements InventoryService {
    
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProductCateDao productCateDao;
    @Autowired
    private SessionUserUtil sessionUserUtil;

    @Override
    public Response list(Integer page, Integer size, String spuNumber, String key, String type) {
        Response response = new Response();
        
        //从session中获取用户id
        String userId = null;
        if(type == null){
            Integer status = StatusUtils.HQ_INVENTORY.getType();
            userId = status.toString();
        }else if(type.equals("own")){
            userId = sessionUserUtil.getUser().getId().toString();
        }else{
            userId = type;
        }
        
        Integer start = (page-1) * size;
        List<Map<String, Object>> product = inventoryDao.list(start, size, spuNumber, key, userId);
        
        if(null != product){
            for(Map<String, Object> map : product){
                int count = productDao.getGoodsBarCountByProductId(map.get("productId"));
                map.put("goodsBarCodeNumber", count);
            }
        }
        
        Integer totalNum = inventoryDao.listAmount(spuNumber, key, userId);
        PageResult<Map<String, Object>> pageList = new PageResult<>(product,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
        
        
    }

    @Override
    public Response createGoodsBarCode(Integer number, Integer productId) {
        Response response = new Response();
        String imageServerBaseUrl = PropUtils.getString("image.server.baseUrl");
        String imagePath = PropUtils.getString("image.path");
        List<String> list = new ArrayList<>();
        for(int i=0;i<number;i++){
            String goodsBar = GenerateUserAccountNumberUtils.generaateUserAccountNumber();
            String url = imagePath + goodsBar + ".jpg";
            String urlNew = imageServerBaseUrl + goodsBar + ".jpg";
            OneBarcodeUtil.createBarcode(goodsBar, new File(url), "");
            list.add(urlNew);
            ProductGoods productGoods = new ProductGoods();
            productGoods.setProductId(productId).setGoodsBarCode(goodsBar).setIsUse(0).setBarCodeUrl(urlNew).setIsPrice(0).setIsDamage(0);
            productDao.insertProductGoods(productGoods);
        }
        response.setData(list);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response inventoryOutList(Integer page, Integer size, String goodsBarCode, String spuNumber, 
                                     String startTime, String endTime, String type) {
        Response response = new Response();
        String userId = sessionUserUtil.getUser().getId().toString();
        if(type.equals("0")){
            //查询总部的出库单
            userId = Integer.toString(-1);
        }else if(type != null && !type.equals("0") && !type.equals("1")){
            userId = type;
        }
        Integer start = (page-1) * size;
        List<Map<String, Object>> product = inventoryDao.inventoryOutList(start, size, goodsBarCode, spuNumber, startTime, endTime, userId);
        Integer totalNum = inventoryDao.inventoryOutListAmount(goodsBarCode, spuNumber, startTime, endTime, userId);
        PageResult<Map<String, Object>> pageList = new PageResult<>(product,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response inventoryInList(Integer page, Integer size, String goodsBarCode, String spuNumber, 
                                    String startTime, String endTime, String type) {
        Response response = new Response();
        String userId = sessionUserUtil.getUser().getId().toString();
        if(type.equals("0")){
            //查询总部的出库单
            userId = Integer.toString(-1);
        }else if(type != null && !type.equals("0") && !type.equals("1")){
            userId = type;
        }
        Integer start = (page-1) * size;
        List<Map<String, Object>> product = inventoryDao.inventoryInList(start, size, goodsBarCode, spuNumber, startTime, endTime, userId);
        Integer totalNum = inventoryDao.inventoryInListAmount(goodsBarCode, spuNumber, startTime, endTime, userId);
        PageResult<Map<String, Object>> pageList = new PageResult<>(product,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }
}
